{% macro pager(page, pages, generator) -%}
{% if pages > 1 %}
<div class="pagination">
    <ul>
    {% set min = page - 3 %}
    {% set max = page + 3 + 1 %}

    {% if min < 0 %}
        {% set max = max - min %}
    {% endif %}
    {% if max >= pages %}
        {% set min = min - max + pages %}
    {% endif %}

    {% if min < 0 %}
        {% set min = 0 %}
    {% endif %}
    {% if max >= pages %}
        {% set max = pages %}
    {% endif %}

    {% if min > 0 %}
    <li>
        <a href="{{ generator(0) }}">&laquo;</a>
    </li>
    {% else %}
    <li class="disabled">
        <a href="#">&laquo;</a>
    </li>
    {% endif %}
    {% if page > 0 %}
    <li>
        <a href="{{ generator(page-1) }}">&lt;</a>
    </li>
    {% else %}
    <li class="disabled">
        <a href="#">&lt;</a>
    </li>
    {% endif %}

    {% for p in range(min, max) %}
    {% if page == p %}
    <li class="active">
        <a href="#">{{ p + 1 }}</a>
    </li>
    {% else %}
    <li>
        <a href="{{ generator(p) }}">{{ p + 1 }}</a>
    </li>
    {% endif %}
    {% endfor %}

    {% if page + 1 < pages %}
    <li>
        <a href="{{ generator(page + 1) }}">&gt;</a>
    </li>
    {% else %}
    <li class="disabled">
        <a href="#">&gt;</a>
    </li>
    {% endif %}
    {% if max < pages %}
    <li>
        <a href="{{ generator(pages - 1) }}">&raquo;</a>
    </li>
    {% else %}
    <li class="disabled">
        <a href="#">&raquo;</a>
    </li>
    {% endif %}
    </ul>
</div>
{% endif %}
{%- endmacro %}

{% macro render_form_fields(form, focus_set=False) %}
    {{ form.hidden_tag() if form.hidden_tag is defined }}

    {% for f in form if f.type != 'HiddenField' and f.type != 'CSRFTokenField' %}
    <div class="control-group{% if f.errors %} error{% endif %}">
      {{ f.label(class='control-label') }}
      <div class="controls">
        <div>
          {% if not focus_set %}
          {{ f(autofocus='autofocus')|safe }}
          {% set focus_set = True %}
          {% else %}
          {{ f()|safe }}
          {% endif %}
        </div>
        {% if f.description %}
        <p class="help-block">{{ f.description }}</p>
        {% endif %}
        {% if f.errors %}
          <ul>
          {% for e in f.errors if e is string %}
            <li>{{ e }}</li>
          {% endfor %}
          </ul>
        {% endif %}
      </div>
    </div>
    {% endfor %}
{% endmacro %}

{% macro form_tag() %}
    <form action="" method="POST" class="form-horizontal"{% if form.has_file_field %} enctype="multipart/form-data"{% endif %}>
      <fieldset>
        {{ caller() }}
      </fieldset>
    </form>
{% endmacro %}

{% macro render_form_buttons(cancel_url, extra=None) %}
    <div class="control-group">
      <div class="controls">
        <input type="submit" class="btn btn-primary btn-large" value="{{ _gettext('Submit') }}" />
        {% if extra %}
        {{ extra }}
        {% endif %}
        {% if cancel_url %}
        <a href="{{ cancel_url }}" class="btn btn-large">{{ _gettext('Cancel') }}</a>
        {% endif %}
      </div>
    </div>
{% endmacro %}

{% macro render_form(form, cancel_url, extra=None) -%}
    {% call form_tag() %}
        {{ render_form_fields(form) }}
        {{ render_form_buttons(cancel_url, extra) }}
    {% endcall %}
{% endmacro %}

